// components/switchTab/switchTab.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    tabArr: {
      type: Array,
      value: []
    },
    tabType: {
      type: String,
      value: "common"
    },
    layoutType: {
      type: String,
      value: "common"
    },
    isLoans: {
      type: Boolean,
      value: false
    },
    sIndex: {
      type: Number,
      value: 0,
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    switchArr: [],
    selectIndex: 0,
    commonArr: [
      {
        title: "新房",
        key: "newHouse",
      },
      {
        title: "二手房",
        key: "second",
      },
      {
        title: "商铺",
        key: "shop",
      },
      {
        title: "论坛",
        key: "dyn",
      },
      {
        title: "楼市文章",
        key: "news",
      },
    ]
  },
  lifetimes: {
    attached: function () {
      // 在组件实例进入页面节点树时执行
      if (this.properties.tabArr.length > 0) {
        this.setData({
          switchArr: this.properties.tabArr,
          selectIndex: this.properties.sIndex,
        })
      } else {
        if (this.properties.tabType == "common") {
          this.setData({
            switchArr: this.data.commonArr
          })
        }
      }


    },
    detached: function () {

    },
  },
  /**
   * 组件的方法列表
   */
  methods: {
    //切换
    switchAction(e) {
      let index = e.currentTarget.dataset.index;
      let item = this.data.switchArr[index];
      this.setData({
        selectIndex: index
      })

      this.triggerEvent("switchTabChange", { item: item, index: index })
    },
  }
})
